package gui;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import owl.DLQueryAgent;

public class MainPanel extends Frame {
	JTextField queryField = new JTextField("please input the query");
	DLQueryAgent dl = new DLQueryAgent();
	private JTextArea textField;

	public MainPanel() {
		super("query");
		setSize(800, 600);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		ActionListener queryListener = new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String str = queryField.getText();
				if (null != str && str.trim().length() != 0) {
					String res = dl.doQuery(str.trim());
					System.out.println(res);
					textField.setText(res);
				}
			}
		};

		Panel gui = new Panel();
		gui.setLayout(null);
		queryField.setBounds(106, 6, 354, 21);
		gui.add(queryField);

		// The "preferred" BorderLayout add call
		add(gui);

		JLabel lblQuery_1 = new JLabel("query:");
		lblQuery_1.setBounds(20, 9, 54, 15);
		gui.add(lblQuery_1);

		JButton btnQuery = new JButton("Query");
		btnQuery.setBounds(494, 5, 93, 23);
		btnQuery.addActionListener(queryListener);
		gui.add(btnQuery);

		JLabel lblResult = new JLabel("Result:");
		lblResult.setBounds(20, 59, 54, 15);
		gui.add(lblResult);
		textField = new JTextArea();
		textField.setLineWrap(true);
		JScrollPane scrollPane = new JScrollPane(textField);
		scrollPane.setBounds(20, 79, 754, 473);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		gui.add(scrollPane);

	}

	public static void main(String args[]) {
		MainPanel tf1 = new MainPanel();
		tf1.setVisible(true);
	}
}